/*
 * @description: 
 * @Date: 2022-11-24 19:18:43
 * @example: 
 * @params: 
 */
import Vue from "vue";
import Vuex from 'vuex';
import { refrshAvatar , refrshNickname, RefrshUserInfo,DeleAvatar } from '@/request/api'
// 持久化插件
import createPersistedState from "vuex-persistedstate"
Vue.use(Vuex);

export default new Vuex.Store({

state:{
    Userinfo:{
        id:"",
        password:"",
        username:"",
        role:"",
        nickname:"",
        avatar:"",
        fileName:""
    },
    MessageList:[],
    token:null,
    Crmbarr:[],
},
getters:{
    id(state){
        return state.Userinfo.id
    },
    Username(state){
        return state.Userinfo.username
    },
    Role(state){
        return state.Userinfo.role
    },
    Nickname(state){
        return state.Userinfo.nickname
    },
    fileName(state){
        return state.Userinfo.fileName
    },
    image(state){
        return state.Userinfo.avatar
    },
    MsgList(state){
        return state.MessageList
    }
},
mutations:{
    updataUserinfo(state,params){
        state.Userinfo = params
        console.log("Vuex",params);
    },
    updatatoken(state,params){
        state.token = params
    },
    updataRouter(state,params){
        state.Crmbarr.push(params)
    },
    /** 点击删除 */
    SplitCrmb(state,params){
      let index =  state.Crmbarr.findIndex((v)=>{
            return v.title == params
        })
      state.Crmbarr.splice(index,1)
    },
    updatehistory(state,params){
        console.log(params)
        state.MessageList.push(params);
    }
},
actions:{
    // 登录之后异步提交到Vuex 
    commitUserinfoasync(context,paylaod){
        context.commit("updataUserinfo",paylaod);
    },
    committoken(context,paylaod){
        context.commit("updatatoken",paylaod);
    },
    commitRouterlinksync(context,paylaod){
        context.commit("updataRouter",paylaod)
    },
    commitRouterSplitsync(context,paylaod){
        context.commit('SplitCrmb',paylaod)
    },
    /** 聊天记录 */
    HandleChathistory({commit},payload){
        commit('updatehistory',payload)
    },
    /** 更新用户头像信息 */
    HandleUpadteAvatar({commit},paylaod){
        const { id , Image,fileName} = paylaod
         refrshAvatar({Image,fileName,id}).then(({data})=>{
                if(data.code === 200 ){
                    RefrshUserInfo({id}).then(({data})=>{
                        if(data.code === 200){
                            console.log(data.data[0])
                            commit('updataUserinfo',data.data[0])
                            console.log("VUEX------------------HandleUpadteAvatar---------------")
                        }
                    })
                }
         }).catch(err=>{

         })
    },
    /** 删除头像信息 */
    HandleDeleteAvatar({commit},paylaod){
        console.log(paylaod)//有输出
        const { id } = paylaod
        DeleAvatar({id}).then(({data})=>{
            if(data.code === 200){
                console.log("删除成功",data);
                RefrshUserInfo({id}).then(({data})=>{
                    if(data.code === 200){
                        console.log(data.data[0])
                        commit('updataUserinfo',data.data[0])
                        console.log("VUEX------------------HandleDeleteAvatar---------------")
                    }
                })
            }
        })
    },
    /** 更新nickname */
    HandleUpadteNickname({commit},paylaod){
        const { nickname,id} = paylaod
        refrshNickname({nickname,id}).then(({data})=>{
            if(data.code === 200){
                RefrshUserInfo({id}).then(({data})=>{
                    if(data.code === 200){
                        console.log(data.data[0])
                        commit('updataUserinfo',data.data[0])
                        console.log("VUEX------------------HandleDeleteAvatar---------------")
                    }
                })
            }
        })
    }
},
modules:{
    
},
plugins: [createPersistedState({
    storage: window.sessionStorage
})]


})
